from kivy.logger import Logger
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from nmjeditor.screens.screen import Screen
from nmjeditor.widgets.dvd import DVDBox
from nmjeditor.widgets.label import ScrollableLabel
from nmjeditor.widgets.wall import ScrollablePosterWall, ScrollableFanartWall



class DetailScreen(Screen):
	def __init__(self, screen_factory, controller, show):
		super(DetailScreen, self).__init__(screen_factory, controller)
		wallpaper = show.wallpaper
		self.show = show
		self.wallpaper = Image(source=wallpaper)
		self.add_widget(self.wallpaper)
		self.dvdbox = DVDBox(show=show, callback=self.want_to_change_poster, size_hint=(None, None), size=(200, 300), pos_hint={'x':.05, 'y':.05})
		self.add_widget(self.dvdbox)
		back = Button(text="Back", pos_hint={'x':.1, 'y':.9}, size_hint=(None, None), size=(200, 50))
		back.bind(on_press=self.on_back)
		self.add_widget(back)
		rescan = Button(text="Not my movie...", pos_hint={'x':.4, 'y':.9}, size_hint=(None, None), size=(200, 50))
		rescan.bind(on_press=self.on_rescan)
		self.add_widget(rescan)
		change_wallpaper = Button(text="Change wallpaper...", pos_hint={'x':.7, 'y':.9}, size_hint=(None, None), size=(200, 50))
		change_wallpaper.bind(on_press=self.want_to_change_wallpaper)
		self.add_widget(change_wallpaper)
		self.controller.get_synopsis(show=show)

	def __reset(self, show):
		self.show = show
		self.wallpaper.source = show.wallpaper
		self.dvdbox.poster.source = show.detail_poster

	def on_synopsis(self, synopsis):
		Logger.info("on_synopsis %s", synopsis)
		self.add_widget(ScrollableLabel(text=synopsis, size_hint=(0.5, 0.5), pos_hint={'x':.5, 'y':.05}))

	def poster_choosen(self, touch, poster):
		self.__popup.dismiss()
		self.__popup = Popup(title="Update", content=Label(text="Poster update in progress\nPlease wait..."),
				auto_dismiss=False)
		self.__popup.open()
		self.controller.update_poster(self.show, poster)

	def wallpaper_choosen(self, touch, wallpaper):
		self.__popup.dismiss()
		self.__popup = Popup(title="Update", content=Label(text="Wallpaper update in progress\nPlease wait..."),
				auto_dismiss=False)
		self.__popup.open()
		self.controller.update_wallpaper(self.show, wallpaper)

	def on_poster_updated(self, poster_path):
		self.dvdbox.poster.source = poster_path
		self.__popup.dismiss()

	def on_wallpaper_updated(self, wallpaper_path):
		self.wallpaper.source = wallpaper_path
		self.__popup.dismiss()

	def remove_popup(self, *args, **kwargs):
		self.__popup.dismiss()
		return True

	def want_to_change_poster(self, touch, show):
		self._create_popup('Loading posters...', self.poster_choosen)
		self.controller.get_posters(show=show)

	def _create_popup(self, title, on_choice, wall_class=ScrollablePosterWall):
		content = FloatLayout()
		cancel = Button(text="Cancel", size_hint=(None, None), size=(200, 50), pos_hint={'x':.01, 'y':.01})
		content.add_widget(cancel)
		self.poster_wall = wall_class(size_hint=(1.0, .8), callback=on_choice, pos_hint={'x':.01, 'y':.15})
		content.add_widget(self.poster_wall)
		self.__popup = Popup(title=title, content=content,
			auto_dismiss=False)
		self.__popup.open()
		cancel.bind(on_press=self.remove_popup)

	def on_poster(self, poster):
		self.__popup.title = "Choose your poster"
		self.poster_wall.add_posters(poster)

	def on_posters(self, posters):
		self.__popup.title = "Choose your poster"
		self.poster_wall.add_posters(*posters)

	def on_wallpaper(self, wallpaper):
		self.__popup.title = "Choose your wallpaper"
		self.poster_wall.add_posters(wallpaper)

	def on_wallpapers(self, wallpapers):
		self.__popup.title = "Choose your wallpaper"
		self.poster_wall.add_posters(*wallpapers)

	def want_to_change_wallpaper(self, value):
		self._create_popup('Loading wallpapers...', self.wallpaper_choosen, wall_class=ScrollableFanartWall)
		self.controller.get_wallpapers(show=self.show)

	def on_back(self, value):
		self.screen_factory.set_screen("Main")

	def movie_choosen(self, touch, poster):
		Logger.info("Choose movie %s", poster.movie.title)
		self.__popup.dismiss()
		self.__popup = Popup(title="Update", content=Label(text="Movie update in progress\nPlease wait..."),
				auto_dismiss=False)
		self.__popup.open()
		self.controller.update_show(self.show, poster.movie)

	def on_show_updated(self, show):

		self.__popup.dismiss()

	def on_search_movie_result(self, movie):
		poster = movie.poster
		poster.movie = movie
		poster.title = movie.title
		self.poster_wall.add_posters(poster)

	def on_rescan(self, value):
		self._create_popup('Searching for movies...', self.movie_choosen)
		self.controller.research(self.show)


